package cn.ito.zjgsu.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.ito.zjgsu.constant.StatusCode;
import cn.ito.zjgsu.domain.DepartmentType;
import cn.ito.zjgsu.service.DepartmentService;
import cn.ito.zjgsu.vo.DepartmentSavaVo;

@Controller
public class DepartmentController {

	@Autowired
	DepartmentService departService;
	
	@RequestMapping(value="/depart")
	public String index() {
		return "sys/depMan";
	}
	
	@RequestMapping(value="/department", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> addDepartment(@RequestBody DepartmentSavaVo depart) {
		Map<String, Object> result = new HashMap<String, Object>();
		departService.addDepart(depart);
		result.put("STATUS", StatusCode.SUCCESS_STATUS);
		return result;
	}
	
	@RequestMapping(value="/department", method = RequestMethod.PUT)
	public @ResponseBody Map<String, Object> updateDepartment(@RequestBody DepartmentSavaVo dep) {
		Map<String, Object> result = new HashMap<String, Object>();
		departService.updateDepart(dep);
		result.put("STATUS", StatusCode.SUCCESS_STATUS);
		return result;
	}
	
	@RequestMapping(value="/department/{departmentId}", method = RequestMethod.DELETE)
	public @ResponseBody Map<String, Object> deleteDepartment(@PathVariable int departmentId) {
		Map<String, Object> result = new HashMap<String, Object>();
		departService.deleteDepart(departmentId);
		result.put("STATUS", StatusCode.SUCCESS_STATUS);
		return result;
	}
	
	@RequestMapping(value="/department/departments", method = RequestMethod.GET)
	public @ResponseBody List<DepartmentType> findAllDepartment() {
//		Map<String, Object> result = new HashMap<String, Object>();
		List<DepartmentType> departments = departService.findAllDepart();
//		result.put("departments", departments);
		return departments;
	}
	
	@RequestMapping(value="/department/departments/{departmentId}", method = RequestMethod.GET)
	public @ResponseBody DepartmentType findDepartmentById(@PathVariable int departmentId) {
		DepartmentType department = departService.findDepartById(departmentId);
		return department;
	}
}
